{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "\n",
    "import env\n",
    "from utils.plot import plot_learning_curves\n",
    "\n",
    "%matplotlib inline\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## load optimizer data and plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ema(w, gamma=0.6):\n",
    "    w_ema = [w[0]]\n",
    "    for i in xrange(1, len(w)):\n",
    "        w_ema.append(gamma * w_ema[-1] + (1. - gamma) * w[i])\n",
    "    return w_ema"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "checkpoint = torch.load('../models/0.8277-5606.ckpt', map_location={'cuda:0': 'cpu'})\n",
    "# checkpoint = torch.load('../main/c7/0.6588-2812.ckpt', map_location={'cuda:0': 'cpu'})\n",
    "\n",
    "train_loss_history = checkpoint['train_loss']\n",
    "train_acc_history = checkpoint['train_acc']\n",
    "val_loss_history = [np.mean(v) for v in checkpoint['val_loss']]\n",
    "val_acc_history = checkpoint['val_acc']\n",
    "\n",
    "# train_loss_history = [[np.mean(v)] for v in train_loss_history]\n",
    "\n",
    "b = 8\n",
    "n = (len(train_loss_history) / b)\n",
    "train_loss_history = train_loss_history[:b*n]\n",
    "train_acc_history = train_acc_history[:b*n]\n",
    "val_loss_history = val_loss_history[:n]\n",
    "val_acc_history = val_acc_history[:n]\n",
    "\n",
    "train_loss_history = [np.mean(v) for v in train_loss_history]\n",
    "train_loss_history = np.asarray(train_loss_history).reshape(-1, b).mean(axis=1).tolist()\n",
    "# train_loss_history = [[v] for v in train_loss_history]\n",
    "train_acc_history = np.asarray(train_acc_history).reshape(-1, b).mean(axis=1).tolist()\n",
    "\n",
    "gamma = 0.9\n",
    "train_loss_history = [[v] for v in ema(train_loss_history, gamma)]\n",
    "train_acc_history = ema(train_acc_history, gamma)\n",
    "val_loss_history = ema(val_loss_history, gamma)\n",
    "val_acc_history = ema(val_acc_history, gamma)\n",
    "\n",
    "plot_learning_curves(train_loss_history, train_acc_history, \n",
    "                     val_loss_history, val_acc_history, min_acc=0.1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
